<template>
  <div class="app-container">
    <el-table :data="productList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="产品名字" align="center" prop="productName" />
      <el-table-column label="商品主图" align="center" prop="pic" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.pic" :width="50" :height="50"/>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </div>
</template>

<script>
import {listProduct} from "@/api/product/product";

export default {
  data() {
    return {
      // 总条数
      total: 0,
      // 产品管理表格数据
      productList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productName: null,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询产品管理列表 */
    getList() {
      this.loading = true;
      listProduct(this.queryParams).then(response => {
        this.productList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.$emit("select",null)
    },

    // 处理单选框选择逻辑
    handleSelectionChange(val) {
      this.selectedRow = val[0];
    },
    /** 提交按钮 */
    submitForm() {
      //this.$emit("select",选中的商品名,)
      if (this.selectedRow) {
        // 获取选中的商品名
        const selectedProductName = this.selectedRow.pic;
        // 将选中的商品名传递给父组件
        this.$emit('select', selectedProductName);
      } else {
        // 提示用户未选择数据
      }
    }
  }
};
</script>
